package com.kd.controller.jobSeeker;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.kd.bean.jobSeeker.Basic_information;
import com.kd.bean.jobSeeker.Doc_resumes;
import com.kd.bean.recruiter.Company_msg;
import com.kd.bean.recruiter.Recruiter_basic;
import com.kd.service.jobSeeker.IBasic_informationService;
import com.kd.service.jobSeeker.IDoc_resumesService;
import com.kd.service.recruiter.ICompany_msgService;

/**
 * 求职者 控制层
 * 
 * @author ZFC
 *
 */
@Controller
@RequestMapping("/basic_information")
public class Basic_informationController {

	@Autowired
	private IBasic_informationService basicService;

	@Autowired
	private ICompany_msgService comService;
	
	@Autowired
	private IDoc_resumesService resService;

	@RequestMapping("/basicLogin")
	public String basicLogin(Basic_information basic, Model model, HttpSession session) {
		// 登录
		try {
			Basic_information basicMessage = basicService.basicLogin(basic);
			if (basicMessage != null) {
				if (basicMessage.getBasic_login_state() == 2) {
					model.addAttribute("msg", "该账号不可登录");
					return "/view/login.jsp";
				}
				session.setAttribute("basicMessage", basicMessage);// 将求职者信息放入session中
				return "/view/main.jsp";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("msg", "该账号不存在，或(账号/邮箱)密码输入错误");
		return "/view/login.jsp";
	}

	@RequestMapping("/basicRegister")
	public String basicRegister(Basic_information basic, Model model, HttpSession session) {
		// 注册
		String msg = "";
		try {
			Boolean registerFlag = basicService.basicRegister(basic);
			msg = registerFlag ? "注册成功" : "注册失败";
			model.addAttribute("msg", msg);
			if (registerFlag) {
				return "/view/login.jsp";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/view/register.jsp";
	}

	@RequestMapping("/retrievePassword")
	@ResponseBody
	public Map<String, Object> retrievePassword(Basic_information basic) {
		Map<String, Object> map = new HashMap<>();
		// 重置密码
		Basic_information baseMessage = null;
		try {
			baseMessage = basicService.retrievePasswordShowPassword(basic);
			map.put("map", baseMessage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}

	@RequestMapping("/quit")
	public String quit(HttpSession session) {
		// 退出
		// 销毁session
		session.invalidate();
		return "redirect:/view/main.jsp";
	}

	@RequestMapping("/queryAllBasic_information")
	@ResponseBody
	public PageInfo<Basic_information> queryAllBasic_information(Integer pageNum, Integer pageSize) {
		// 管理员操作分页显示所有应聘者信息
		PageInfo<Basic_information> info = null;
		try {
			info = basicService.queryAllBasic_information(pageNum, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return info;
	}

	@RequestMapping("/updateBasic_login_state")
	@ResponseBody
	public Boolean updateBasic_login_state(Integer login_state, Integer basic_id) {
		// 管理员操作修改应聘者状态。禁用者不可登录，启用者可正常登录
		Boolean flag = false;
		try {
			flag = basicService.updateBasic_login_state(login_state, basic_id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@RequestMapping("/queryBasicPersonCount")
	@ResponseBody
	public Integer queryBasicPersonCount() {
		// 管理员求职人数
		Integer basicCount = 0;
		try {
			basicCount = basicService.queryBasicPersonCount();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return basicCount;
	}

	@RequestMapping("/queryByJudge_deliver_resumAndLikeRec_address")
	@ResponseBody
	public PageInfo<Basic_information> queryByJudge_deliver_resumAndLikeRec_address(Integer pageNum, Integer pageSize,
			HttpSession session) {
		Recruiter_basic basic = (Recruiter_basic) session.getAttribute("basicMsg");

		PageInfo<Basic_information> info = null;
		try {
			Company_msg company = comService.queryByCompany_id(basic.getCompany_id());// 通过公司id查询公司信息
			info = basicService.queryByJudge_deliver_resumAndLikeRec_address(pageNum, pageSize, company);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return info;
	}

	@RequestMapping("/queryByBasic_id")
	@ResponseBody
	public Basic_information queryByBasic_id(Integer basic_id) {
		// 查询应聘者 通过应聘者id 管理员

		Basic_information basic = null;
		try {
			basic = basicService.queryByBasic_id(basic_id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return basic;
	}

	
	@RequestMapping("/queryDetailMessageByBasic_id")
	@ResponseBody
	public Basic_information queryDetailMessageByBasic_id(HttpSession session) {
		// 查询应聘者 通过应聘者id 应聘者

		Basic_information basic = null;
		Basic_information basic_information = (Basic_information) session.getAttribute("basicMessage");
		try {
			basic = basicService.queryByBasic_id(basic_information.getBasic_id());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return basic;
	}
	
	@RequestMapping("/updateBasic_information")
	public String updateBasic_information(Basic_information basic_information,Model model,
			@RequestParam("myFile") MultipartFile myFile, HttpServletRequest request,HttpSession session) {
		Boolean flag = false;
		// 1、设置上传路径
		String storPath = System.getProperty("user.dir") + "/data/tmp/image";
		//获得上传路径转成file类型
		File file = new File(storPath);
		// 2、判断该位置存在不存在
		if (!file.getParentFile().exists()) {// 如果父路径不存在
			file.mkdirs();// 则创建目录
		}
		try {
			// 3、将文件上传到上传路径
			File file2 = new File(storPath,myFile.getOriginalFilename());
			myFile.transferTo(file2);
			basic_information.setBasic_photo(myFile.getOriginalFilename());
			flag = basicService.updateBasic_information(basic_information);
			if(flag) {
				Basic_information basicMessage = basicService.queryByBasic_id(basic_information.getBasic_id());
				//修改成功重新存值
				session.setAttribute("basicMessage",basicMessage);
				model.addAttribute("msg", "修改成功");
				return "/view/detail.jsp";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("msg", "修改失败");
		return "/view/detail.jsp";
	}
	
	@RequestMapping("/updateBasic_informationPwd")
	public String updateBasic_informationPwd(String basic_password,String jiu_ps,HttpSession session,Model model) {
		Basic_information basic_information = (Basic_information) session.getAttribute("basicMessage");
		try {
			Basic_information basicMsg = basicService.queryByBasic_id(basic_information.getBasic_id());
			if(jiu_ps.equals(basicMsg.getBasic_password())) {
				//旧密码等于新密码
				if(basicMsg.getBasic_password().equals(basic_password)) {
					//旧密码等于修改的密码
					model.addAttribute("msg", "新密码与旧密码不能一样哦！");
					return "/view/detail.jsp";
				}else {
					Basic_information information = new Basic_information();
					information.setBasic_password(basic_password);
					information.setBasic_id(basic_information.getBasic_id());
					Boolean flag = basicService.updateBasic_informationPwd(information);
					model.addAttribute("msg", flag?"修改成功":"修改失败");
					if(flag) {
						return "/view/login.jsp";
					}
					return "/view/detail.jsp";
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("msg", "与原始密码不一样哦！");
		return "/view/detail.jsp";
	}
	@RequestMapping("/updateBasic_informationBasic_job_state")
	public String updateBasic_informationBasic_job_state(Integer basic_job_state,HttpSession session,Model model) {
		Boolean flag = false;
		Basic_information basicMessage = (Basic_information) session.getAttribute("basicMessage");
		try {
			Basic_information basic_information = basicService.basicLogin(basicMessage);
			if(basic_job_state==2) {
				if(basic_information.getRes_id()!=null) {
					List<Doc_resumes> list = resService.queryAllByBasic_id(basic_information.getBasic_id());
					if(list.size()!=0) {
						flag = basicService.updateBasic_informationBasic_job_state(basic_job_state, basic_information.getBasic_id());
					}else {
						model.addAttribute("msg","请完善文档简历后在修改求职状态！");
						return "/view/detail.jsp";
					}
				}else {
					model.addAttribute("msg","请完善在线简历后在修改求职状态！");
					return "/view/detail.jsp";
				}
			}else {
				flag = basicService.updateBasic_informationBasic_job_state(basic_job_state, basic_information.getBasic_id());
			}
			model.addAttribute("msg", flag?"修改成功":"修改失败");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/view/detail.jsp";
	}
}
